﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoftwareClass_1
{
    public partial class Editform : Form
    {
        public Editform()
        {
            InitializeComponent();
        }
        private int Id;
        public  Editform(int id ,string tiltle,string author)
        {
            InitializeComponent();
            Id = id;
            textBox1.Text = tiltle;
            textBox2.Text = author.ToString();
        }
        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = textBox1.Text;
            var author = textBox2.Text;

            //修改
            if (Id > 0)
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("update course set Title = '{0}',Author = '{1}' where id = {2}", title, author, Id);
                    var res = DBhelper.AddOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                        this.Close();
                    }
                }
                //添加
                else
                {
                    this.DialogResult = DialogResult.No;
                    this.Close();
                }
            }
            else
            {

                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Articles (Title,Author) values ('{0}',{1});", title, author);
                    var res = DBhelper.AddOrDelete(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                        this.Close();
                    }

                }
                else
                {
                    MessageBox.Show("你未填写任何数据", "提示");
                }


            }
        }

            /// <summary>
            /// 取消
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
